package com.level2app.view.popWindow;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.level2app.databinding.PopupwindowTipsBinding;

/*
 * Author: S
 * Date: 2024/9/9
 */
public class TipsPopupWindow extends PopupWindow {

    private final PopupwindowTipsBinding binding;

    public TipsPopupWindow(Context context) {
        binding = PopupwindowTipsBinding.inflate(LayoutInflater.from(context));
        this.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        setContentView(binding.getRoot());
    }

    /**
     * 显示在控件正上方
     */
    public void showPopUp(View view) {
        binding.getRoot().measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int[] location = new int[2];
        // 获取控件在屏幕上的位置
        view.getLocationOnScreen(location);
        int popWidth = binding.getRoot().getMeasuredWidth();
        int popHeight = binding.getRoot().getMeasuredHeight();
        showAtLocation(view, Gravity.NO_GRAVITY, (location[0] + view.getWidth() / 2) - popWidth / 2, location[1] - popHeight);
    }
}